<?php
class LogControl extends Object implements ISingleton
{
    public $debugLog;

    protected static $instance;

    public function LogControl()
    {
        parent::__construct();
        $this->RenderDebugLog();
    }

    public function RenderDebugLog()
    {
        $this->debugLog = $this->RenderRequestLogs().$this->RenderServerLogs();
    }

	public static function GetInstance()
	{
		return self::$instance ? self::$instance : self::$instance = new LogControl();
	}

    public function RenderSqlLog()
    {
    	if (empty(Log::$sql)) return;
		$total_time = 0;
    	$report[] =
    		"<tr>
    			<td></td>
    			<td>rows</td>
    			<td>ms</td>
    			<td>query</td>
    		</tr>";
    	foreach (Log::$sql as $key => $log)
    	{
    		$index = $key+1;
    		$time = $log['time'];
    		$sql = trim($log['sql']);
    		//$sql = String::HighlightSql($sql);
    		$report[] =
    			"<tr>
    				<td>$index.</td>
    				<td>{$log['affected']}</td>
    				<td>$time</td>
    				<td>$sql</td>
    			</tr>";
    		$total_time += $time;
    	}
    	$report = implode("\n", $report);
		$query_types = $this->RenderSqlQueryTypes();
		return "<fieldset class=\"sql\"><legend>$index queries - $total_time ms</legend><p>$query_types</p><table cellpadding=0 cellspacing=0>$report</table></fieldset>";
    }

    public function RenderErrorLog()
    {
    	if (0 == $count = count(Log::$errors)) return;
    	$errors = null;
    	foreach (Log::$errors as $key => $err)
    	{
    		$index = $key+1;
    		$message = $err['message'];
    		$trimmed_msg = trim($message);
    		$errors .= "$index)$trimmed_msg\n";
    	}
		return "<fieldset class=\"error\"><legend>$count errors</legend><pre>$errors</pre></fieldset>";
    }

    public function RenderSqlErrorLog()
    {
    	if (0 == $count = count(Log::$sqlErrors)) return;
    	$errors = null;
    	foreach (Log::$sqlErrors as $key => $err)
    	{
    		$index = $key+1;
    		$message = $err['message'];
    		$trimmed_msg = trim($message);
    		$errors .= "$index)$trimmed_msg\n";
    	}
		return "<fieldset class=\"sql_error\"><legend>$count sql errors</legend><pre>$errors</pre></fieldset>";
    }

    public function RenderVarsLog()
    {
    	if (0 == $count = count(Log::$vars)) return;
    	$logs = null;
    	foreach (Log::$vars as $var)
    	{
    		$logs .= getdebug($var['value'], $var['label']);
    	}
		return "<fieldset class=\"debug_var\"><legend>Debug variables</legend>$logs</fieldset>";
    }

    public function Render()
    {
        $requestLogs = $this->RenderRequestLogs();
		$sql_error_logs = $this->RenderSqlErrorLog();
		$error_logs = $this->RenderErrorLog();
		$var_logs = $this->RenderVarsLog();
		$sql_logs = $this->RenderSqlLog();
        $serverLogs = $this->RenderServerLogs();
    	$bg_color = count(Log::$sqlErrors) + count(Log::$errors) ? 'red':'green';
    	$label = RequestHandler::GetInstance()->url.' - <span style="font-size:110%;color:yellow">'. RequestHandler::GetInstance()->elapsedTime.'</span> ms';

    	return AppConfig::GetInstance()->SIGN_DEBUG.
    		"<div class='log'>
    			<span class='toggle' onclick=\"this.next('.logcontent').toggle()\" style='background-color:$bg_color'>$label</span>
    			<div class='logcontent' style='display:none;border-color:$bg_color'>$var_logs $error_logs $sql_error_logs $requestLogs $sql_logs $serverLogs</div>
			</div>";
    }

    public function RenderSqlQueryTypes()
    {
    	$out = array();
    	foreach (Log::$sqlQueryTypes as $type => $tables)
		{
            $count = array_sum(Set::Pluck($tables, 'count'));
			$out[] = "$type($count): ".implode(', ', Set::Pluck($tables, 'label'));
		}
		return implode("<br/>", $out);
    }

    public function RenderRequestLogs()
    {
        return "<fieldset class='svr_var'><legend>Requests</legend>".getdebug($_GET, 'GET ').getdebug($_POST, 'POST ').getdebug($_FILES, 'FILES ')."</fieldset>";
    }

    public function RenderServerLogs()
    {
        return "<fieldset class='svr_var'><legend>Server variables</legend>".
            getdebug($_COOKIE, 'COOKIE ').
            getdebug($_SESSION, 'SESSION ').
            getdebug($_SERVER, 'SERVER ').
        "</fieldset>";
    }
}
?>
